package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.CurriculumResources;
import com.production.CoursePlatform.service.CurriculumResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.List;


@RestController
@RequestMapping("/curriculumResources")
public class CurriculumResourcesController {
    //此接口已检验
    @Autowired
    private CurriculumResourcesService curriculumResourcesService;

    // 增加资源不需要rsID
    @PostMapping("/addWithoutRsID")
    public ResponseEntity<String> addResourceWithoutRsID(@RequestBody CurriculumResources resource) {
        boolean success = curriculumResourcesService.saveCurriculumResource(resource);
        if (success) {
            return ResponseEntity.ok("Resource added successfully without rsID");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add resource");
        }
    }

    // 删除资源
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteResource(@PathVariable int id) {
        boolean success = curriculumResourcesService.deleteCurriculumResourceBYrsID(id);
        if (success) {
            return ResponseEntity.ok("Resource deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete resource");
        }
    }

    // 更新资源
    @PutMapping("/update")
    public ResponseEntity<String> updateResource(@RequestBody CurriculumResources resource) {
        boolean success = curriculumResourcesService.updateCurriculumResource(resource);
        if (success) {
            return ResponseEntity.ok("Resource updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update resource");
        }
    }

    // 查询单个资源
    @GetMapping("/get/{id}")
    public ResponseEntity<CurriculumResources> getResource(@PathVariable int id) {
        CurriculumResources resource = curriculumResourcesService.getCurriculumResourceById(id);
        if (resource != null) {
            return ResponseEntity.ok(resource);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 按ctID查找资源
    @GetMapping("/getByCtID/{ctID}")
    public ResponseEntity<List<CurriculumResources>> getResourcesByCtID(@PathVariable int ctID) {
        List<CurriculumResources> resources = curriculumResourcesService.getAllCurriculumResources(ctID);
        return ResponseEntity.ok(resources);
    }
}

